/*
 * ikakeibo 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.model;

import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

import com.wateray.ipassbook.domain.Subject;

/**
 * @author wateray
 * @create 2009-5-9
 */
public class SubjectListCellRender extends DefaultListCellRenderer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** The child subjects. indent + child. */
	String indent = "  ";
	static String leftParenthesis = "[";
	static String rightParenthesis = "]";

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing
	 * .JList, java.lang.Object, int, boolean, boolean)
	 */
	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		// TODO Auto-generated method stub
		list.setPrototypeCellValue("Index 1234567890");
		
		PassbookComboBoxModel listModel = (PassbookComboBoxModel) list.getModel();
		Subject sub = (Subject) listModel.getObjectAt(index);

		if (sub != null) {
			if (sub.isChild()) {
				value = indent + sub.getName();
			} else {
				if (sub.hasChild()) {
					value = leftParenthesis + sub.getName() + rightParenthesis;
				} else {
					value = sub.getName();
				}
			}
		}
		setText(value == null ? "" : value.toString());
		
		return this;
	}

}
